﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using Core = Vision.Core;

namespace Vision.Core.ActivityComponents.IO.Vfs
{
    /// <summary>
    /// 
    /// </summary>
    public class VfsFileOpen : NativeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        public InArgument<string> CatalogConnection { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public InArgument<string> Key { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public OutArgument<string> Name { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public OutArgument<string> Description { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public OutArgument<int> FolderId { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public OutArgument<byte[]> FileData { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            Entities.Filing.Document document = null;
            using (Core.Components.ActivityCatalog documentCatalog = new Core.Components.ActivityCatalog(this.CatalogConnection.Get(context))){
                document = documentCatalog.GetVisionDocumentFromCatalog(Guid.Parse(context.GetValue(this.Key)));       
            }

            if (document != null){
                context.SetValue(this.Name, document.Name);
                context.SetValue(this.Description, document.Description);
                context.SetValue(this.FolderId, document.FolderId);
                context.SetValue(this.FileData, document.Data);
            }                        
        }
    }
}
